﻿using Minecraft.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Minecraft.Model;
using System.Text.RegularExpressions;

namespace Minecraft.DALMySql
{
	public class GoodsDAL : BaseDAL
	{
		/// <summary>
		/// 向goods表插入数据（分表插入，如果不存在表，则先建立表，并将表名称缓存起来）
		/// </summary>
		/// <param name="model"></param>
		/// <param name="goodsTableNameCacheList"></param>
		/// <returns></returns>
		public static bool InsertSuccessForSplitTable(GoodsModel model, List<string> goodsTableNameCacheList)
		{
			return InsertSuccessModelData(model,
				goodsTableNameCacheList,
				model.PlayerId,
				TablePrefixKey.Goods,
				JsonConfig.Value.Mysql.SubmeterLen.GoodsTable,
				GetPrefixTableSql);
		}

		public static GoodsModel GetFirstOrDefault(int playerId, string goodsId)
		{
			var model = new GoodsModel();
			return GetSingleOrDefaultWithTablePrefix(model, playerId,
				MemorySystemManager.goodsTableNameCacheList,
				TablePrefixKey.Goods,
				JsonConfig.Value.Mysql.SubmeterLen.GoodsTable,
				new KeyValuePair<string, string>(nameof(model.GoodsId), goodsId), GetPrefixTableSql);
		}

		public static List<GoodsModel> GetListAll(int playerId)
		{
			var model = new GoodsModel();
			return GetListAllWithTablePrefix(model, playerId,
				MemorySystemManager.goodsTableNameCacheList,
				TablePrefixKey.Goods,
				JsonConfig.Value.Mysql.SubmeterLen.GoodsTable,
				new KeyValuePair<string, int>(nameof(model.PlayerId), playerId), GetPrefixTableSql);
		}

		/// <summary>
		/// 批量更新goods表（先删除，后统一插入）
		/// </summary>
		/// <param name="list"></param>
		/// <param name="playerId"></param>
		/// <param name="belongsTo"></param>
		/// <returns></returns>
		public static bool BatchUpdateWithTablePrefix(List<GoodsModel> list,
			List<string> goodsTableNameCacheList,
			int playerId, EnumGoodsBelongsToType enumGoodsBelongsToType)
		{
			var model = new GoodsModel();
			return BatchUpdateWithTablePrefix(list,
				goodsTableNameCacheList,
				playerId,
				TablePrefixKey.Goods,
				JsonConfig.Value.Mysql.SubmeterLen.GoodsTable,
				new KeyValuePair<string, int>(nameof(model.PlayerId), playerId),
				new KeyValuePair<string, int>(nameof(model.BelongsTo), (int)enumGoodsBelongsToType),
				GetPrefixTableSql);
		}

		/// <summary>
		/// 批量更新goods表（先删除，后统一插入）
		/// </summary>
		/// <param name="list"></param>
		/// <param name="playerId"></param>
		/// <param name="belongsTo"></param>
		/// <returns></returns>
		public static bool BatchUpdateWithTablePrefix(List<GoodsModel> list,
			List<string> goodsTableNameCacheList,
			int playerId)
		{
			var model = new GoodsModel();
			return BatchUpdateWithTablePrefix(list,
				goodsTableNameCacheList,
				playerId,
				TablePrefixKey.Goods,
				JsonConfig.Value.Mysql.SubmeterLen.GoodsTable,
				new KeyValuePair<string, int>(nameof(model.PlayerId), playerId),
				GetPrefixTableSql);
		}
	}
}
